package com.share.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.device.domain.Region;
import com.share.device.mapper.RegionMapper;
import com.share.device.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: 滴滴滴
 * @CreateTime: 2025-03-28
 * @Description:
 * @Version: 1.0
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {
    @Autowired
    private RegionMapper regionMapper;

    //由于在rouyi框架中，已经做了封装，注入，直接使用baseMapper调用即可。所以这里注入可以不手动注入
//    @Autowired
//    private RegionMapper regionMapper;
    //根据上级code获取下级元素列表
    @Override
    public List<Region> treeSelect(String code) {
        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Region::getParentCode, code);
        List<Region> regionList = baseMapper.selectList(wrapper);

        //判断是否有下一层的数据，如果有着数据库中hasChildren=true，否则false
        if (!CollectionUtils.isEmpty(regionList)){
            //1.把regionList遍历，得到每一个Region
            regionList.forEach(region -> {
                //2.查询每一个Region对象是否有下一层数据parent_code=？
                //如果有下一层数据，设置hasChildren=true，否则false
                LambdaQueryWrapper<Region> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(Region::getParentCode, region.getCode());
                Long count = baseMapper.selectCount(wrapper1);
                if (count > 0){
                    //有下一层数据
                    region.setHasChildren(true);
                }else {
                    region.setHasChildren(false);
                }
            });
        }
        return regionList;
    }

    @Override
    public String getNameByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return "";
        }
        Region region = regionMapper.selectOne(new LambdaQueryWrapper<Region>().eq(Region::getCode,code).select(Region::getName));
        if(null != region) {
            return region.getName();
        }
        return "";
    }
}
